const { Sequelize } = require('sequelize')
const initModels = require('../models/init-models');
const { mysql } = require('./DataBase');

/**
 * 函数描述：数据库日志输出
 * @param {*} msg 
 */
const logging = (msg) => {
  console.log(' ')
  console.log('*************************************************')
  console.log('****   持久化执行日志：                      ****')
  console.log('****   ' + msg)
  console.log('*************************************************')
  console.log(' ')
}

const sequelize = new Sequelize(mysql.database, mysql.username, mysql.password, {
  host: mysql.host,
  dialect: mysql.dialect,
  pool: { max: mysql.pool.max, min: mysql.pool.min, acquire: mysql.pool.acquire, idle: mysql.pool.idle },
  define: { timestamps: false },// 不自动生成时间
  timezone: mysql.timezone,
  logging: logging,
  dialectOptions: mysql.dialectOptions
});

(async function () {
  try {
    await sequelize.authenticate()
    console.log('*************************************************')
    console.log('****   数据库连接成功                        ****')
    console.log('*************************************************')
  } catch (error) {
    console.log('*************************************************')
    console.log('****   数据库连接失败                        ****')
    console.log('*************************************************')
  }
})()

const models = initModels(sequelize);

module.exports = {
  sequelize,
  ...models
}